home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / pur_c_vi.zoo / linefunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-11  |  4.6 KB  |  251 lines

  1. /*
  2.  * STevie - ST editor for VI enthusiasts.    ...Tim Thompson...twitch!tjt...
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "stevie.h"
  7.  
  8. /*
  9.  * nextline(curr)
  10.  *
  11.  * Return a pointer to the beginning of the next line after the
  12.  * 'curr' char.  Return NULL if there is no next line.
  13.  */
  14.  
  15. char *
  16. nextline(curr)
  17. char *curr;
  18. {
  19.     while ( curr<Fileend ) {
  20.         if ( *curr++ == '\n' )
  21.             break;
  22.     }
  23.     if ( curr >= Fileend )
  24.         return(NULL);
  25.     return(curr);
  26. }
  27.  
  28. /*
  29.  * prevline(curr)
  30.  *
  31.  * Return a pointer to the beginning of the previous line before the
  32.  * 'curr' char.  Return NULL if there is no previous line.
  33.  */
  34.  
  35. char *
  36. prevline(curr)
  37. char *curr;
  38. {
  39.     int nnl = 0;
  40.  
  41.     /* If we are currently positioned on a '\n', */
  42.     /* we are on a blank line.  Adjust accordingly. */
  43.     if ( *curr == '\n' )
  44.         nnl = -1;
  45.     while ( curr>Filemem ) {
  46.         /* look for the 2nd previous newline */
  47.         if ( *curr == '\n' ) {
  48.             nnl++;
  49.             if ( nnl == 2)
  50.                 break;
  51.         }
  52.         curr--;
  53.     }
  54.     if ( curr <= Filemem ) {
  55.         /* If we found 1 newline, we found the first line */
  56.         if ( nnl == 1 )
  57.             return(Filemem);
  58.         else
  59.             return(NULL);
  60.     }
  61.     /* return the first char of the previous line */
  62.     return(++curr);
  63. }
  64.  
  65. /*
  66.  * coladvance(p,col)
  67.  *
  68.  * Try to advance to the specified column, starting at p.
  69.  */
  70.  
  71. char *
  72. coladvance(p,col)
  73. char *p;
  74. int col;
  75. {
  76.     int c, inc;
  77.  
  78.     /* If we're on a blank ('\n' only) line, we can't do anything */
  79.     if ( *p == '\n' )
  80.         return(p);
  81.     /* try to advance to the specified column */
  82.     for ( c=0; col-- > 0; c++ ) {
  83.         /* Count a tab for what it's worth */
  84.         if ( *p == '\t' ) {
  85.             inc = (7 - c%8);
  86.             col -= inc;
  87.             c += inc;
  88.         }
  89.         p++;
  90.         /* Don't go past the end of */
  91.         /* the file or the line. */
  92.         if ( p==Fileend || *p=='\n' ) {
  93.             p--;
  94.             break;
  95.         }
  96.     }
  97.     return(p);
  98. }
  99. char *strcpy(), *malloc();
  100.  
  101. #define NULL 0
  102.   
  103. char *
  104. alloc(size)
  105. unsigned size;
  106. {
  107.     char *p;        /* pointer to new storage space */
  108.  
  109.     p = malloc(size);
  110.     if ( p == (char *)NULL ) {    /* if there is no more room... */
  111.         message("alloc() is unable to find memory!");
  112.     }
  113.     return(p);
  114. }
  115.  
  116. char *
  117. strsave(string)
  118. char *string;
  119. {
  120.     return(strcpy(alloc((unsigned)(strlen(string)+1)),string));
  121. }
  122.  
  123. #define NULL 0
  124.  
  125. static char *laststr = NULL;
  126. static int lastdir;
  127.  
  128. char *
  129. ssearch(dir,str)
  130. int dir;    /* FORWARD or BACKWARD */
  131. char *str;
  132. {
  133.     if ( laststr != NULL )
  134.         free(laststr);
  135.     laststr = strsave(str);
  136.     lastdir = dir;
  137.     if ( dir == BACKWARD )
  138.         return(bcksearch(str));
  139.     else
  140.         return(fwdsearch(str));
  141. }
  142.  
  143. dosearch(dir,str)
  144. int dir;
  145. char *str;
  146. {
  147.     char *p;
  148.  
  149.     if ( (p=ssearch(dir,str)) == NULL )
  150.         message("Pattern not found");
  151.     else {
  152.         char *savep;
  153.  
  154.         cursupdate();
  155.         /* if we're backing up, we make sure the line we're on */
  156.         /* is on the screen. */
  157.         Curschar = savep = p;
  158.         /* get to the beginning of the line */
  159.         beginline();
  160.         if ( Curschar < Topchar )
  161.             Topchar = Curschar;
  162.         Curschar = savep;
  163.         cursupdate();
  164.         updatescreen();
  165.     }
  166. }
  167.  
  168.  
  169. repsearch()
  170. {
  171.     if ( laststr == NULL )
  172.         beep();
  173.     else {
  174.         dosearch(lastdir,laststr);
  175.     }
  176. }
  177.  
  178. char *
  179. fwdsearch(str)
  180. char *str;
  181. {
  182.     register char *sofar = str;
  183.     register char *infile = Curschar+1;
  184.     int leng = strlen(str);
  185.     char *stopit;
  186.  
  187.     /* search forward to the end of the file */
  188.     for ( ; infile < Fileend && *sofar != '\0' ; infile++ ) {
  189.         if ( *infile == *sofar )
  190.             sofar++;
  191.         else
  192.             sofar = str;
  193.     }
  194.     if ( *sofar == '\0' )
  195.         return(infile-strlen(str));
  196.     /* search from the beginning of the file to Curschar */
  197.     infile = Filemem;
  198.     sofar = str;
  199.     stopit = Curschar + leng;
  200.     for ( ; infile <= stopit && *sofar != '\0' ; infile++ ) {
  201.         if ( *infile == *sofar )
  202.             sofar++;
  203.         else
  204.             sofar = str;
  205.     }
  206.     if ( *sofar == '\0' )
  207.         return(infile-leng);
  208.     else
  209.         return(NULL);
  210. }
  211.  
  212. char *
  213. bcksearch(str)
  214. char *str;
  215. {
  216.     int leng = strlen(str);
  217.     char *infile = Curschar+1;
  218.     char *endofstr, *sofar, *stopit;
  219.  
  220.     /* make sure str isn't empty before getting pointer to */
  221.     /* its last character. */
  222.     if ( leng == 0 )
  223.         return(NULL);
  224.     endofstr = &str[leng-1];
  225.     sofar = endofstr;
  226.     /* search backward to the beginning of the file */
  227.     for ( ; infile >= Filemem && sofar >= str ; infile-- ) {
  228.         if ( *infile == *sofar )
  229.             sofar--;
  230.         else
  231.             sofar = endofstr;
  232.     }
  233.     if ( sofar < str )
  234.         return(++infile);
  235.  
  236.     /* search backward from the end of the file */
  237.     sofar = endofstr;
  238.     infile = Fileend-1;
  239.     stopit = Curschar - leng;
  240.     for ( ; infile >= stopit && sofar >= str ; infile-- ) {
  241.         if ( *infile == *sofar )
  242.             sofar--;
  243.         else
  244.             sofar = endofstr;
  245.     }
  246.     if ( sofar < str )
  247.         return(++infile);
  248.     else
  249.         return(NULL);
  250. }
  251.